package cn.com.template.controller.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.dal.dataobject.hr.UserDO;
import cn.com.service.hr.UserService;
import cn.com.support.context.UserContext;
import cn.com.support.message.MessageResult;
import cn.com.support.utils.JacksonUtil;

/**
 * 
 * 用户信息控制器
 * 
 * @since 1.0 2012-2-21 下午11:03:53
 * @version $Id$
 * @author Zoe
 */
@Controller
public class UserInfoController {

	
	@Autowired
	private UserService userService;
	
	@RequestMapping("help")
	public ModelAndView showdHelpInfo(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("help");
		return mav;
	}
	
	@RequestMapping("userinfo")
	public ModelAndView showdUserInfo(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView("userinfo");
		request.setAttribute("user", UserContext.getUser());
		//加载用户角色信息
		return mav;
	}
	
	@RequestMapping("/updatePw")
	@ResponseBody
	public MessageResult<Integer> updatePW(@RequestParam String newpw,HttpServletRequest request){
		MessageResult<Integer> result = null;
		UserDO userDO = null;
		try {
			userDO = UserContext.getUser();
			userDO.setPassword(newpw);
			userDO.setModifier(userDO.getNickname());
			result = userService.updateById(userDO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping("/updateUser")
	@ResponseBody
	public MessageResult<Integer> updateUser(HttpServletRequest request){
		MessageResult<Integer> result = null;
		UserDO userDO = null;
		try {
			UserDO curUser = UserContext.getUser();
			String jsonData = request.getParameter("data");
			userDO = (UserDO) JacksonUtil.json2Bean(jsonData, UserDO.class);
			userDO.setId(curUser.getId());
			userDO.setModifier(curUser.getUsername());
			result = userService.updateById(userDO);
			//需要添加刷新session user对象方法
			//等待登录模块完成...
			userDO = userService.selectById(curUser.getId());
			UserContext.setUser(userDO);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping("/checkCurPw")
	@ResponseBody
	public boolean checkCurrentPw(@RequestParam String curpw, HttpServletRequest request){
		boolean flag = false;
		try {
			//验证当前密码
			UserDO userDO = UserContext.getUser();
			UserDO queryDO = new UserDO();
			queryDO.setId(userDO.getId());
			queryDO.setPassword(curpw);
			
			List<UserDO> userLst = userService.selectList(queryDO);
			if(userLst.size()==1){
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

}
